'use strict';

const Service = require('egg').Service;
const md5 = require('md5');

class UserService extends Service {
  // 登录
  async login({ username, password }) {
    try {
      let user = await this.app.model.User.findOne({
        where: { username, password: md5(password) },
        attributes: {
          exclude: ['password', 'projectName', 'rolenameName', 'className', 'cruxnameName']
        }
      });
      if (user) {
        user = JSON.stringify(user);
        user = JSON.parse(user);
        const access_token = this.app.jwt.sign(user, this.app.config.jwt.secret, {
          expiresIn: '2h',
        });
        return {
          code: 2000,
          msg: '成功',
          access_token,
          username: user,
        };
      }
      return {
        code: 2001,
        msg: '用户名密码错误',
      };

    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
  // 获取用户数据
  async getUser({ limit, offset }) {
    try {
      let data = await this.app.model.User.findAll({
        limit: Number(limit) || 10, // 当页条数
        offset: Number(offset) * Number(limit) || 0, // 开始下标
        attributes: {
          exclude: ['password', 'projectName', 'rolenameName']
        },
      });
      for (let i = 0; i < data.length; i++) {
        const { username, name, sex, email, phone, state, project_name, rolename_name, createAt, updateAt } = data[i];
        const Class = await this.app.model.Userclass.findAll({
          where: { user_name: data[i].name },
          attributes: ['class_name'],
        })
        data[i] = { username, name, sex, email, phone, state, project_name, rolename_name, Class, createAt, updateAt }
      }
      return {
        code: 2000,
        msg: '成功',
        data
      };
    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
  // 模糊查询
  async getVague(vague) {
    try {
      const Op = require('sequelize').Op;
      const data = await this.app.model.User.findAll({
        where: { username: { [Op.like]: `%${vague}%` } },
        attributes: {
          exclude: ['password', 'projectName', 'rolenameName']
        }
      });
      for (let i = 0; i < data.length; i++) {
        const { username, name, sex, email, phone, state, project_name, rolename_name, createAt, updateAt } = data[i];
        const Class = await this.app.model.Userclass.findAll({
          where: { user_name: data[i].name },
          attributes: ['class_name'],
        })
        data[i] = { username, name, sex, email, phone, state, project_name, rolename_name, Class, createAt, updateAt }
      }
      return {
        code: 2000,
        msg: '成功',
        data
      };
    } catch (error) {
      return await this.app.Lostconnection(error.message);
    }
  }
  //查询 添加用户获取项目、角色、班级列表数据
  async gitCreateUser() {
    const project = await this.app.model.Project.findAll({ attributes: ['name'] });
    const rolename = await this.app.model.Rolename.findAll({ attributes: ['name'] });
    return {
      project,
      rolename,
    };
  }
  async gitCreateClass() {
    const project = this.ctx.query.project;
    const Class = await this.app.model.Class.findAll({
      where: { project_name: project ,state:'有效'},
      attributes: ['name'],
    });
    return Class;
  }
  //添加用户数据
  async createUser() {
    const { username, name, sex, email, phone, state, project_name, rolename_name, Class } = this.ctx.request.body;
    await this.app.model.User.create({
      username, name,
      password: 'e10adc3949ba59abbe56e057f20f883e',
      sex, email, phone, state, project_name,
      rolename_name
    });
    for (let i = 0; i < Class.length; i++) {
      await this.app.model.Username.create({
        user_name: name, class_name:Class[i].name
      })
    }
    return {
      code:200,
      msg:'添加成功',
      // data
    }
  }
}

module.exports = UserService;

